<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  iphone
 */

class m_iphone_services_IphoneService extends f_core_Service
{
	/**
	 * @var m_backoffice_services_IphoneService
	 */
	private static $instance;
    private $xml;

	/**
	 * Constructor
	 */
	private function __construct()
	{
        header('content-type: application/xml');
        
		$xml = new XMLWriter();
        $xml->openMemory();
        $xml->startDocument('1.0', 'UTF-8');
        $xml->writeDtd('plist', '-//Apple//DTD PLIST 1.0//EN', 'http://www.apple.com/DTDs/PropertyList-1.0.dtd');
        $xml->setIndent(true);
        $xml->setIndentString('    ');
        $xml->startElement('plist');
        $xml->writeAttribute('version', '1.0');

        $this->xml = $xml;
	}

	/**
	 * @return m_backoffice_services_IphoneService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}

    public function getConstructorIphone()
    {
        return $this->xml;
    }

    public function genericConfirm($error, $msg = false)
    {
        if(!$msg)
        {
            f_locale_Manager::getLocale('backoffice.errors.error');
        }
        
        $xml = $this->getConstructorIphone();
        $xml->startElement('dict');
        $xml->writeElement('key', 'result');
        if($error)
        {
            $xml->writeElement('false');
            $xml->writeElement('key', 'errormsg');
            $xml->writeElement('string', $msg);
        }
        else
        {
            $xml->writeElement('true');
        }
        $xml->endElement();
        $xml->endElement();
        $xml->endDocument();

        return $xml->outputMemory();
    }

    /**
     *  Iphone authentification
     * @param <type> $user
     * @return <type> user's rights access at xml format for iphone
     */
    public function login()
    {
        $xml = $this->xml;

        $xml->startElement('dict');
            $xml->writeElement('key', 'result');

            $userContext = $this->getUserContext();
            $user = $userContext->getUser();

            if($user)
            {
                $xml->writeElement('true');
                $xml->writeElement('key','rows');
                $xml->startElement('array');
                    $xml->startElement('dict');
                        $xml->writeElement('key', 'username');
                        $xml->writeElement('string', $user->getFirstName() . ' ' . $user->getLastName());
                        $xml->writeElement('key', 'userid');
                        $xml->writeElement('integer', $user->getUniqueId());
                        $xml->writeElement('key', 'rights');
                        $xml->startElement('array');

                        $modules = array();
                        $rights = $userContext->getRights();
                        foreach($rights as $right => $bool)
                        {
                            $module = explode('_', $right);
                            $moduleName = $module[0];
                            unset($module[0]);
                            $modules[$moduleName][] = join('_', $module);
                        }

                        foreach($modules as $key => $module)
                        {
                            $xml->startElement('dict');
                                $xml->writeElement('key', 'module');
                                $xml->writeElement('string', $key);
                                $xml->writeElement('key', 'actions');
                                $xml->startElement('array');
                                
                                foreach($module as $right)
                                {
                                    $xml->startElement('dict');
                                        $xml->writeElement('key', 'action');
                                        $xml->writeElement('string', $right);
                                    $xml->endElement();
                                }

                                $xml->endElement();
                            $xml->endElement();
                        }

                        $xml->endElement();
                    $xml->endElement();
                $xml->endElement();
            }
            else
            {
                $xml->writeElement('false');
                $xml->writeElement('key', 'errormsg');
                $xml->writeElement('string', f_locale_Manager::getLocale('backoffice.errors.loginFailed'));
            }

        $xml->endElement();
        $xml->endDocument();

        return $xml->outputMemory();
    }

    /**
     *  Get the modules list for iphone
     * @return XML code
     */
    public function getIphoneModuleList()
    {
        $xml = $this->xml;
        $xml->startElement('dict');
            $xml->writeElement('key', 'modules');
            $xml->startElement('array');

            /* MAIN MODULES */
            $modulesBase = Array('medias'
                                    => array('Médias', '')
                                );

            foreach($modulesBase as $key => $module)
            {
                $xml->startElement('dict');
                    $xml->writeElement('key', 'title');
                    $xml->writeElement('string', $module[0]);
                    $xml->writeElement('key', 'module');
                    $xml->writeElement('string', $key);
                    $xml->writeElement('key', 'icon');
                    $xml->writeElement('string', $module[1]);
                $xml->endElement();
            }

            /* ADD-ON MODULES */
            $instanceDir = f_file_Directory::getInstance(MODULES_DIR);
            $modulesDir = $instanceDir->getSubdirs();

            $modules = array();

            foreach($modulesDir as $moduleDir)
            {
                $xmlPath = $moduleDir->getPath().DS.'config'.DS.'iphone.xml';
                $xmlVersion = f_file_File::getInstance($xmlPath);

                if(is_file($xmlPath))
                {
                    $xmlParse = f_xml_Xpath::getInstance($xmlPath);
                    $moduleName = $xmlParse->getElementValueFromXpath('name');

                    $xml->startElement('dict');

                        $rootNode = m_backoffice_services_RootNodeService::getInstance()
                                                ->getModuleRootNode($moduleName);

                        $xml->writeElement('key', 'title');
                        $xml->writeElement('string', $rootNode->getLabel());
                        $xml->writeElement('key', 'module');
                        $xml->writeElement('string', $rootNode->getModule());
                        $xml->writeElement('key', 'icon');

                        $route = f_core_Routing::getUrl('moduleMedia', array('moduleName' => $rootNode->getModule(), 'mediaName' => $rootNode->getIcon()));
                        $src = URL_ABSOLUTE.'/bo'.$route;

                        $xml->writeElement('string', $src);
                    $xml->endElement();
                }
            }

            $xml->endElement();
        $xml->endElement();
        $xml->endDocument();

        return $xml->outputMemory();
    }
}
